# -*- coding: utf-8 -*-
"""
Created on Tue Aug 14 20:43:43 2018

@author: Hy
"""

import requests
import re


def get_html_text(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    }
    try:
        r = requests.get(url, timeout=30, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except Exception as e:
        print(e)


def get_datas(url):
    text = get_html_text(url)
    regex = re.compile(r'<td class="no-wrap currency-name" data-sort="([\s\S]*?)">')
    datas = re.findall(regex, text)
    for x in datas:
        with open('bizhong.txt', 'a', encoding='utf-8') as f:
            f.write(x.replace(' ', '-') + '\n')


url = 'https://coinmarketcap.com/all/views/all/'
get_datas(url)
